<template>
	<view>
		<view class="listBox">
			<view class="item" v-for="(item,index) in list" :key="index" @click="xuanzedizhi(item)">
				<view class="top">
					<text class="name">{{item.real_name}}</text>
					<text class="phone">{{item.phone}}</text>
				</view>
				<view class="center">

					<text class="addres">
						<text class="moren" v-if="index == 0">默认</text>
						{{item.province}}{{item.city}}{{item.district}}
					</text>
					<text class="bianji" @click="bianji(index)">编辑</text>
					<text class="shanchu" @click="deleteFun(item)">删除</text>
				</view>
			</view>
		</view>
		<view class="pushaddres" @click="gobianji">
			添加收货地址
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				info: null,
				first: false,
				xuanze: false
			};
		},
		onLoad(data) {
			if (data.xz) {
				this.xuanze = true
			}
			uni.getStorage({
				key: 'userinfo',
				success: (res) => {
					this.info = res.data;
					this.first = true;
					this.getAddres();
				}
			})
		},
		onShow() {
			if (this.first) {
				this.getAddres();
			}
		},
		methods: {
			gobianji() {
				uni.navigateTo({
					url: '../bianjidizhi/bianjidizhi'
				})
			},
			getAddres() {
				uni.request({
					url: this.$AJAX_URL + 'my/useraddr',
					method: 'POST',
					data: {
						uid: this.info.uid
					},
					success: (res) => {
						console.log(res);
						this.list = res.data.data
					}
				})
			},
			deleteFun(item) {
				uni.request({
					url: this.$AJAX_URL + 'my/deladdr',
					data: {
						uid: this.info.uid,
						addrid: item.id
					},
					success: (res) => {
						console.log(res);
						if (res.data.code === 200) {
							this.getAddres()
							this.$u.toast(res.data.msg)
						} else {
							this.$u.toast(res.errMsg)
						}
					}
				})
			},
			bianji(index) {
				uni.navigateTo({
					url: '../bianjidizhi/bianjidizhi?index=' + index
				})
			},
			xuanzedizhi(item) {
				if (this.xuanze) {
					uni.setStorage({
						key: 'addres',
						data: item,
						success: () => {
							uni.navigateBack()
						}
					})
				}
			}


		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}

	.listBox {
		padding-top: 20rpx;
		margin-bottom: 100rpx;
		background: #FFFFFF;

		.item {
			height: 202rpx;
			padding-left: 40rpx;

			.top {
				.name {
					font-size: 32rpx;
					color: #000;
				}

				.phone {
					color: #666666;
					font-size: 28rpx;
					margin-left: 30rpx;
				}
			}

			.center {
				margin-top: 34rpx;

				.moren {
					display: inline-block;
					width: 60rpx;
					height: 30rpx;
					font-size: 24rpx;
					text-align: center;
					line-height: 30rpx;
					color: #FF423F;
					border: 1px solid #FF423F;
					margin-right: 18rpx;
				}

				.noneclass {
					opacity: 0;
				}

				.addres {
					font-size: 32rpx;
					color: #000000;
					margin-left: 18rpx;
					display: inline-block;
					width: 498rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					line-height: 32rpx;
					white-space: nowrap;
				}

				.bianji {
					font-size: 32rpx;
					color: #999999;
					margin-left: 20rpx;
				}

				.shanchu {
					font-size: 32rpx;
					color: #FF423F;
					margin-left: 40rpx;
				}
			}
		}
	}

	.pushaddres {
		width: 100%;
		height: 98rpx;
		background-color: #FF423F;
		text-align: center;
		line-height: 98rpx;
		font-size: 32rpx;
		color: #FFFFFF;
		position: fixed;
		bottom: 0;
	}
</style>
